<template>
	<form class="createGroup" @submit="formSubmit" @reset="formReset">
		<view class="uni-form-item uni-column">
			<view class="title">用户昵称</view>
			<view class="item">
				<input placeholder-style="font-size: 14px;" type="text" name="nickname" placeholder="如张三、李四" :value="selfInfo.nickname" />
			</view>
		</view>
		<!-- <view class="uni-form-item uni-column">
			<view class="title">密码</view>
			<view class="item">
				<input placeholder-style="font-size: 14px;" type="text" name="password" :password="true" :value="selfInfo.password" />
			</view>
		</view> -->
		<view class="uni-form-item uni-column">
			<view class="title">手机号</view>
			<view class="item">
				<input placeholder-style="font-size: 14px;" name="mobile" type="number" placeholder="138xxxxxxxx" :value="selfInfo.mobile" maxlength="11" />
			</view>
		</view>
		<view class="uni-form-item uni-column">
			<view class="title">邮箱</view>
			<view class="item">
				<input placeholder-style="font-size: 14px;" type="text" name="email" placeholder="xxx@yyy.com" :value="selfInfo.email" />
			</view>
		</view>
		<view class="uni-btn-v">
			<button type="default" form-type="reset">重置修改</button>
			<button :loading="false" form-type="submit">提交修改</button>
		</view>
	</form>
</template>

<script>
	export default {
		data() {
			return {
				selfInfo: {
					nickname: "",
					// password: "",
					mobile: "",
					email: ""
				}
			};
		},
		created() {
			var uid = uni.getStorageSync("uid")
			// console.log("account id: " + uid)

			uniCloud.callFunction({
				name: "ha_get_account",
				data: { uid },
				success: data => {
					// console.log(data)
					if(data.result.status === 0){
						this.selfInfo = data.result.data;
						// uni.showToast({
						// 	title: `成功！`,
						// 	duration: 1000
						// });
					}else{
						uni.showToast({
							icon: 'none',
							title: `${data.result.msg || "网络异常，请稍后重试！"}`,
							duration: 1000
						});
					}
				},
				fail() {
					uni.showToast({
						icon: 'none',
						title: '网络异常，请稍后重试！'
					});
				}
			});
		},
		methods: {
			formSubmit(e) {
				e.detail.value.uid = uni.getStorageSync("uid")
				// console.log(
				// 	"form发生了submit事件，携带数据为：" + JSON.stringify(e.detail.value)
				// );

				uni.showLoading({
					title: '修改中...',
					mask: true
				});

				uniCloud.callFunction({
					name: "ha_update_account",
					data: e.detail.value,
					success: data => {
						console.log(data)
						if (data.result.code === 0) {
							console.log("success!");
							uni.showToast({
								title: `${data.result.msg}`,
								duration: 1000
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: `${data.result.msg || "网络异常，请稍后重试！"}`,
								duration: 1000
							});
						}
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '网络异常，请稍后重试！'
						});
					}
				});
			},
			formReset() {
				this.selfInfo = this.selfInfo;
			}
		}
	};
</script>

<style lang="less">
	.uni-form-item {
		padding: 5rpx 0;
	}

	.createGroup__name {
		height: 50rpx;
		border: 1rpx solid #666;
	}

	.createGroup .item {
		padding: 5rpx 12rpx;
	}

	.createGroup .item input {
		padding-left: 24rpx;
		height: 80rpx;
		border: 1rpx solid rgba(102, 102, 102, 0.5);
	}

	.createGroup .text {
		font-size: 14px;
	}

	view {
		color: #666 !important;
	}
</style>
